package com.hsylient.javase.chapter02.practice.demo02.version02;

import lombok.NonNull;
import lombok.SneakyThrows;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;

public abstract class Extractor {

    /**
     * 校验锁提取号码的格式
     *
     * @return 正确的号码格式
     */
    public abstract String checkPattern(String resource);

    @SneakyThrows
    public List<String> extract(@NonNull File sourceFile) {
        Scanner scanner;
        try {
            scanner = new Scanner(sourceFile);
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException(String.format("文件未找到，文件名称：%s", sourceFile.getName()));
        }
        List<String> target = new ArrayList<>();
        while (scanner.hasNext()) {
            String resource = scanner.next();
            String checkPattern = checkPattern(resource);
            if (Objects.isNull(checkPattern)) {
                continue;
            }
            target.add(checkPattern);
        }
        return target;
    }
}
